import breadcrumb from '@/components/common/breadcrumb'
import download from '@/components/common/download'
import log from '@/components/common/log'
import fixedbutton from '@/components/common/fixedbutton'
import PersonSelect from '@/components/asc/PersonSelect'
import procApprove from '@/components/common/procApprove'

import {submitWorkListItem,getWinDetailsByProcInfo,findFileList,getWinDetailsById,copySendItemSubmitSecond} from '@/api/api.js'
import { type } from 'os'

export default {
  components:{
    breadcrumb,
    download,
    log,
    fixedbutton,
    PersonSelect,
    procApprove
  },
  data(){
    return {
      showFileBrand:false,
      brand:[
        {name: 'lang.workbench'},
        {name: 'lang.need_deal_task_details'}
      ],
      win:{},
      //流程详情
      flowDetails:this.$route.query.obj,
      hasIcon:false,
      isOpen1:"1",
      isOpen2:"1",
      isOpen3:"1",
      procInstId:this.$route.params.task_id,
      dataList:[],
      fileProps:[
        {prop:'fileRealName',label:'文件名'},
        {prop:'createTime',label:'上传时间'},
      ],
      showRead:this.$route.query.obj.isHasReadTask,//显示待阅还是待办
      flow_access:{
        staffNoBefore: this.$route.query.obj.preActivitySender,//上一审批人
        actId:this.$route.query.obj.activityID,
        procId: this.$store.state.proc.procId,
        procChName:this.$store.state.proc.procName,//流程中文名
        task_type:'',//任务类型
        mergeTask:'0',//合并任务
        slotCount:'1',//任务处理规则
        resign:'',//会签类型
        reselMan:'',//是否重选快速审批人
        act_name:this.$route.query.obj.activityName,//当前节点名称
        now_deal_act:(this.$route.query.obj.activityName.split('_'))[0],//当前节点
        deal_answer:'1',//同意 还是退回
        next_deal_act:'',//下一节点
        next_deal_name:[],//下一处理人名称
        deal_suggest:'同意!',//审批意见
        copygive:[],//抄送人
        approveMan:'',//当前提交人 自己
        procGuid:this.$route.query.obj.formInstID,
        procInstId:this.$route.query.obj.procInstID,
        sn:this.$route.query.obj.SN,
        isEnd:this.$route.query.obj.isEnd,
        backActInfo:this.$route.query.obj.showBack == true? this.$route.query.obj.backActInfo : null,
        backAct:this.$route.query.obj.showBack == true? this.$route.query.obj.backAct[0] : null,
        backMan:this.$route.query.obj.showBack == true? this.$route.query.obj.backMan[0] : null,
        backManName:this.$route.query.obj.showBack == true? this.$route.query.obj.backManName[0] : null,
      },
      //下一环节处理  这个应该从服务端获取
      options:[
      ],
      //下一处理人  这个应该根据流程从服务端获取
      dealmans:[
   
      ],
      //这个信息应该从后台中获取
      reward:{
        level:'',
        limit:'',
        name:'',
        recollection:'',
        status:''
      },
      fileList:[
      ]

    }
  },
  methods: {
    back:function(){
      this.$router.back();
    },
    back1:function(){
      this.$router.push('/need_read_list2');
    },
    commitProc:function(){

      //下一步处理人 设置
      if(this.flow_access.deal_answer != "2" && !this.flow_access.isEnd){
        if(document.getElementById("appMan") != null && document.getElementById("appMan").value != ""){
            this.flow_access.next_deal_name = document.getElementById("appMan").value.split(',');  
        }else{
            // this.flow_access.next_deal_name = "";
        }     
      }
      //判断 下步处理环节 和 处理结论不能为空
      if(this.flow_access.deal_answer == ""){
        this.$alert(this.$t('ndt.selDealSuggest'),this.$t('cm.tips'));
        return ;
      }
      if(this.flow_access.next_deal_act == ""){
        this.$alert(this.$t('ndt.selNextAct'),this.$t('cm.tips'));
        return ;
      }
      if(this.flow_access.next_deal_name == "" && this.flow_access.backMan == null){
        this.$alert(this.$t('ndt.selNextDealer'),this.$t('cm.tips'));
        return ;
      }

      //设置任务类型、会签规则、会签类型、是否重选快速审批人 http://cudflow-d
      let selNextAct = this.flowDetails.actOptions.find(n =>{
          return (n.actMetadata == this.flow_access.next_deal_act);
      })
      if(this.flow_access.deal_answer != "2"){
        this.flow_access.task_type = selNextAct.taskActType;
        this.flow_access.mergeTask = selNextAct.isMergeTask;
        this.flow_access.slotCount = selNextAct.taskProcessRule;
        this.flow_access.resign = selNextAct.reSignType;
        this.flow_access.reselMan = selNextAct.isReselFastDealer;
      }else{
        //如果为退回节点  也要设置
        let backNodeInfo = this.flow_access.backActInfo.find(obj => {
          return obj.actMetadata == this.flow_access.next_deal_act
        });
        this.flow_access.task_type = backNodeInfo.taskActType;
        this.flow_access.mergeTask = backNodeInfo.isMergeTask;
        this.flow_access.slotCount = backNodeInfo.taskProcessRule;
        this.flow_access.resign = backNodeInfo.reSignType;
        this.flow_access.reselMan = backNodeInfo.isReselFastDealer;
      }
      
      //处理抄送人
      let copygiveMans = document.getElementById("sendToMan").value;
      let copygiveIds = [];
      if(copygiveMans !=""){
          let mans = copygiveMans.split(',');
          for(let i = 0; i < mans.length; i++){
              copygiveIds.push(mans[i].substr(1,mans[i].lastIndexOf(']')-1));
          }
          this.flow_access.copygive = copygiveIds.join(",");
      }

      let _this = this;
      submitWorkListItem(this.flow_access).then((result) => {
        if(result.status == "200"){
          _this.$alert(_this.$t('ndt.operateSuccess'),_this.$t('cm.tips'),{
            callback: _ => {
              _this.$router.push('/need_deal_task');
            }
          })
        }
      }).catch((err) => {
        _this.$alert(err,_this.$t('cm.tips'));
      });
      
    },
    initData:function(){
      //传递数据给 提交模块
      this.flowDetails = this.$route.query.obj;
      let obj = this.$route.query.obj;
      this.flow_access.act_name = obj.activityName; 
      this.flow_access.procInstId = obj.procInstID;  
      this.flow_access.sn = obj.SN;  

      let _this = this;
      let params = {
        fullName:obj.processFullName,
        procInsId:obj.procInstID,
      }
      

      //获取兑奖信息
      //默认的formInstID为1  原来是通过procGuid来完成的  现在用formInstID保存业务ID
      let id = obj.procGuid;
        getWinDetailsById(obj.formInstID).then((result) => {
          if(result.status == "200"){
            let data = result.data.data;
            _this.win = data;
            if(_this.win.cudBonusProperty != null){
              _this.win.cudBonusProperty = _this.win.cudBonusProperty.split(',');
            }
            _this.getFileList(_this.win.cudWinId);
          }
        }).catch((err) => {
          _this.$alert(err,_this.$t('cm.tips'));
        });
      
    },
    getFileList:function(winId){
      let _this = this;
      findFileList({winId:winId}).then((result) => {
          if(result.status == "200" && result.data.code =="0"){
              _this.dataList = result.data.data;
          }else{
              _this.$alert(result.data.msg,_this.$t('cm.tips'));
          }
      }).catch((err) => {
          _this.$alert(err,_this.$t('cm.tips'));
      });
    },
    //自动待阅提交
    autoHasRead:function(){
      let _this = this;
      copySendItemSubmitSecond({comment:"已阅",workId:_this.$route.query.obj.sn}).then((result) => {
          if(result.status == "200" && result.data.code == "0"){

          }else{
              _this.$alert(result.data.msg,_this.$t('cm.tips'));
          }
      }).catch((err) => {
          _this.$alert(err,_this.$t('cm.tips'));
      }); 
    },
    //待阅提交
    hasRead:function(){
      let _this = this;
      copySendItemSubmitSecond({comment:"已阅",workId:_this.$route.query.obj.procInstID}).then((result) => {
          if(result.status == "200" && result.data.code == "0"){
            _this.$router.back();
          }else{
              _this.$alert(result.data.msg,_this.$t('cm.tips'));
          }
      }).catch((err) => {
          _this.$alert(err,_this.$t('cm.tips'));
      }); 
    }
  },
  mounted() {
    //获取 列表传过来的参数， 通过参数来获取附件信息和流程日志
    this.initData();
    if(this.$route.query.flow != undefined){
      this.autoHasRead();//自动待阅提交
    }else{
      //错误请跳转页面
    }
  },
}
